package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-08-27 9:28
 */
public class DeliveryAddressDaoImpl implements DeliveryAddressDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    private int row=0;

    /**
     * DeliveryAddressController/listDeliveryAddressByUserId
     * 参数：userId
     * 返回值：deliveryAddress数组
     * 功能：根据用户编号查询所属送货地址
     */
    @Override
    public List<Deliveryaddress> listDeliveryAddressByUserId(String userId) throws SQLException {
        List<Deliveryaddress> list = new ArrayList<>();
//        String sql = "SELECT userId,con   tactName, address FROM deliveryaddress WHERE userId=?";
        String sql = "SELECT * FROM deliveryaddress WHERE userId=? order by daId";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);
            rs = pst.executeQuery();
            while (rs.next()) {
                Deliveryaddress das = new Deliveryaddress();
                das.setDaId(rs.getInt("daId"));
                das.setContactName(rs.getString("contactName"));
                das.setContactSex(rs.getInt("contactSex"));
                das.setContactTel(rs.getString("contactTel"));
                das.setAddress(rs.getString("address"));
                das.setUserId(rs.getString("userId"));
                list.add(das);
            }

        } finally {
            DBUtil.close(rs, pst);
        }
        return list;
    }

    /**
     * DeliveryAddressController/getDeliveryAddressById
     * 参数：daId
     * 返回值：deliveryAddress对象
     * 功能：根据送货地址编号查询送货地址
     */
    @Override
    public Deliveryaddress getDeliveryAddressById(int daId) throws SQLException {
        Deliveryaddress das = new Deliveryaddress();
        String sql = "SELECT * FROM deliveryaddress WHERE daId=?";

        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, daId);
            rs = pst.executeQuery();
            while (rs.next()) {

                das.setDaId(rs.getInt("daId"));
                das.setAddress(rs.getString("address"));
                das.setContactName(rs.getString("contactName"));
                das.setContactSex(rs.getInt("contactSex"));
                das.setContactTel(rs.getString("contactTel"));
                das.setUserId(rs.getString("userId"));
            }

        } finally {
            DBUtil.close(rs, pst);
        }
        return das;
    }

    /**
     * DeliveryAddressController/saveDeliveryAddress
     * 参数：contactName、contactSex、contactTel、address、userId
     * 返回值：int（影响的行数）
     * 功能：向送货地址表中添加一条记录
     */
    @Override
    public int saveDeliveryAddress(String contactName, int contactSex, String contactTel, String address, String userId) throws SQLException {
        Deliveryaddress das=null;
        String sql="INSERT INTO deliveryaddress (contactName,contactSex,contactTel,address,userId) VALUES(?,?,?,?,?)";
        try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setString(1,contactName);
            pst.setInt(2,contactSex);
            pst.setString(3,contactTel);
            pst.setString(4,address);
            pst.setString(5,userId);
            int i = pst.executeUpdate();
//            if(i==1){
//                das.setContactName(contactName);
//                das.setContactSex(contactSex);
//                das.setContactTel(contactTel);
//                das.setAddress(address);
//                das.setUserId(userId);
//            }

            return i;
        }finally {
            DBUtil.close(rs,pst);
        }
    }
    /**
     *  DeliveryAddressController/updateDeliveryAddress
     *    参数：daId、contactName、contactSex、contactTel、address、userId
     *    返回值：int（影响的行数）
     *    功能：根据送货地址编号更新送货地址信息
     */
    @Override
    public int updateDeliveryAddress(int daId, String contactName, int contactSex, String contactTel, String address, String userId) throws SQLException {
        String sql="UPDATE deliveryaddress SET contactName=?,contactSex=?,contactTel=?,address=?,userId=? WHERE daId=?";
        try{
            //获取连接
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setString(1,contactName);
            pst.setInt(2,contactSex);
            pst.setString(3,contactTel);
            pst.setString(4,address);
            pst.setString(5,userId);
            pst.setInt(6,daId);
             row = pst.executeUpdate();

             return row;

        }finally {
            DBUtil.close(rs,pst);
        }

    }
    /**
     * DeliveryAddressController/removeDeliveryAddress
     *    参数：daId
     *    返回值：int（影响的行数）
     *    功能：根据送货地址编号删除一条记录
     */
    @Override
    public int removeDeliveryAddress(int daId) throws SQLException {
        String sql="DELETE FROM deliveryaddress WHERE daId =?";
        try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setInt(1,daId);
            row=pst.executeUpdate();
            return row;
        }finally {
            DBUtil.close(rs,pst);
        }

    }
}
